//
//  XMRealNameAuthenView.m
//  XMJJ_zml
//
//  Created by apple on 2025/5/20.
//

#import "XMRealNameAuthenView.h"

@interface XMRealNameAuthenView ()

@property (nonatomic, strong) UITextField *realNameTextField;
@property (nonatomic, strong) UITextField *userIDNumTextField;
@property (nonatomic, strong) UILabel *explainLabel;

@property (nonatomic, strong) UIButton *selectBtn;

@end

@implementation XMRealNameAuthenView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self creatUI];
    }
    return self;
}

- (void)creatUI {
    [self addSubview:self.navView];
    self.navView.backgroundColor = kColorHEX(0xffffff);
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(28.0), kNavagationBarH + KScaleWidth(12.0), self.width - KScaleWidth(28.0)*2, KScaleWidth(46.0)) font:KFontSize(24.0) textColor:kColorHEX(0x221232)];
    titleLabel.text = @"实名认证";
    [self addSubview:titleLabel];
    
    UILabel *explainLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(28.0), titleLabel.maxY, self.width - KScaleWidth(28.0), KScaleWidth(23.0)) font:KFontSize(14.0) textColor:kColorHEX(0x988DA2)];
    _explainLabel = explainLabel;
//    explainLabel.text = [NSString stringWithFormat:@"请填写与注册账号159****8689完成一致的实名信息"];
    [self addSubview:explainLabel];
    
    [self addSubview:self.realNameTextField];
    [self addSubview:self.userIDNumTextField];
    _realNameTextField.y = explainLabel.maxY + KScaleWidth(85.0);
    _userIDNumTextField.y = _realNameTextField.maxY + KScaleWidth(16.0);
    
    [self addSubview:self.selectBtn];
    UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(_selectBtn.maxX + KScaleWidth(5.0), _selectBtn.y, self.width - _selectBtn.maxX - KScaleWidth(5.0), _selectBtn.height)];
    NSMutableAttributedString *agreementAttr = [[NSMutableAttributedString alloc] initWithString:@"我已同意并知晓了《实名认证服务协议》" attributes:@{NSFontAttributeName : KFontSize(13.0), NSForegroundColorAttributeName : kColorHEX(0x988DA2)}];
    [agreementAttr addAttributes:@{NSForegroundColorAttributeName : kColorHEX(0x3B5998)} range:NSMakeRange(8, 10)];
    [textLabel setAttributedText:agreementAttr];
    [self addSubview:textLabel];
    
    CGFloat width = [@"我已同意并知晓了" getWidthWithFont:KScaleWidth(13.0)];
    UIButton *agreementBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    agreementBtn.frame = CGRectMake(textLabel.x + width, textLabel.y, KScaleWidth(140), textLabel.height);
//    agreementBtn.backgroundColor = kColorHEX(0x00ffff);
    [agreementBtn addTarget:self action:@selector(agreementAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:agreementBtn];
    
    UIButton *faceRecoBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KBoldFontSize(17.0) frame:CGRectMake((self.width - KScaleWidth(320.0))/2, _selectBtn.maxY + KScaleWidth(13.0), KScaleWidth(320.0), KScaleWidth(44.0)) title:@"认证"/*@"进入人脸识别" */titleColor:kColorHEX(0x333333)];
    [faceRecoBtn addTarget:self action:@selector(faceRecoAction:) forControlEvents:UIControlEventTouchUpInside];
    [faceRecoBtn setGradientLayerWithLocations:@[@(0), @(1.0f)] colors:@[(__bridge id)kColorHEX(0xFFD800).CGColor, (__bridge id)kColorHEX(0xFFE44E).CGColor] startPoint:CGPointMake(0.5, 0.37) endPoint:CGPointMake(0.03, 0.37)];
    [faceRecoBtn setCornerRadius:faceRecoBtn.height/2];
    [self addSubview:faceRecoBtn];
}

- (void)setContentWithUserModel:(XMUserInfoModel *)userInfoModel {
    if (userInfoModel.phone && userInfoModel.phone.length > 7) {
        NSString *mobileStr = [userInfoModel.phone stringByReplacingCharactersInRange:NSMakeRange(3, 4) withString:@"****"];
        _explainLabel.text = [NSString stringWithFormat:@"请填写与注册账号%@完成一致的实名信息", mobileStr];
    }
}

//服务协议
- (void)agreementAction:(UIButton *)button {
    self.checkServiceInfoBlock ? self.checkServiceInfoBlock() : nil;
}

//人脸识别
- (void)faceRecoAction:(UIButton *)button {
    if (_realNameTextField.text.length == 0) {
        [MBProgressHUD showError:@"请输入真实姓名"];
        return;
    }
    else if (_userIDNumTextField.text.length == 0) {
        [MBProgressHUD showError:@"请输入身份证号吗"];
        return;
    }
    self.realNameAuthenBlock ? self.realNameAuthenBlock(_realNameTextField.text, _userIDNumTextField.text) : nil;
}

#pragma mark - Init
- (UITextField *)realNameTextField {
    if (!_realNameTextField) {
        _realNameTextField = [[UITextField alloc] initWithFrame:CGRectMake(KScaleWidth(28.0), 0, self.width - KScaleWidth(28.0)*2, KScaleWidth(44.0))];
        _realNameTextField.font = KFontSize(16.0);
        _realNameTextField.placeholder = @"请输入真实姓名";
        _realNameTextField.backgroundColor = kColorHEX(0xF5F3F7);
        [_realNameTextField setCornerRadius:_realNameTextField.height/2];
        
        UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScaleWidth(16.0), _realNameTextField.height)];
        _realNameTextField.leftViewMode = UITextFieldViewModeAlways;
        _realNameTextField.leftView = leftView;
    }
    return _realNameTextField;
}

- (UITextField *)userIDNumTextField {
    if (!_userIDNumTextField) {
        _userIDNumTextField = [[UITextField alloc] initWithFrame:CGRectMake(KScaleWidth(28.0), 0, self.width - KScaleWidth(28.0)*2, KScaleWidth(44.0))];
        _userIDNumTextField.font = KFontSize(16.0);
        _userIDNumTextField.placeholder = @"请输入真实姓名";
        _userIDNumTextField.backgroundColor = kColorHEX(0xF5F3F7);
        [_userIDNumTextField setCornerRadius:_userIDNumTextField.height/2];
        
        UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScaleWidth(16.0), _userIDNumTextField.height)];
        _userIDNumTextField.leftViewMode = UITextFieldViewModeAlways;
        _userIDNumTextField.leftView = leftView;
    }
    return _userIDNumTextField;
}

- (UIButton *)selectBtn {
    if (!_selectBtn) {
        _selectBtn = [UIButton buttonWithType:UIButtonTypeCustom frame:CGRectMake(KScaleWidth(55.0), self.height - kTabBarH - KScaleWidth(10.0) - KScaleWidth(44.0) - KScaleWidth(13.0) - KScaleWidth(26.0), KScaleWidth(26.0), KScaleWidth(26.0)) image:@"login_service_select_icon" selectImg:@"login_service_select_icon_selected"];
    }
    return _selectBtn;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
